﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

using ClassLibrary;
using Core;

namespace TestProject
{
    public partial class GraphicWindow : Form
    {
        private Model model;
        private Species sp;
        private Population pop;
        public GraphicWindow(Model m)
        {
            InitializeComponent();            
            model = m;
        }
        decimal done=0;       
        private void draw_chart()
        {

            decimal last = numericUpDown2.Value;
            decimal first = numericUpDown1.Value;
            chart.Series.Clear();
            string str = comboBox1.SelectedItem.ToString();
            List<PopulationStatistics> popstat = pop.population_statistic_list;
            Series series = chart.Series.Add(str);
            series.ChartType = SeriesChartType.Line;
            series.BorderWidth = 4;
            if (last > done)
            {
                this.Cursor = Cursors.WaitCursor;
                progressBar1.Value=0;
                progressBar1.Maximum = (int)(last - done+1);
                for (int i = (int)done; i <= last; i++)
                {
                    model.ModelOneStep();
                    progressBar1.Value++;
                }
                this.Cursor = Cursors.Default;
                done = numericUpDown2.Value;
            }
            switch (str)
            {
                case "Amount of individuals":
                    for (int i = (int)first; i <= last; i++)
                    {
                        series.Points.AddXY(i, popstat[i].number_of_individuals);
                    }
                    break;
                case "Allele frequences":
                    GeneType gt = null;
                    foreach (GeneType g in sp.sp_gene_table.gene_table)
                    {
                        if (comboBox2.SelectedItem.ToString() == g.name)
                            gt = g;
                    }
                    for (int i = (int)first; i <= last; i++)
                    {
                        series.Points.AddXY(i, popstat[i].gene_alleles_frequencies[gt.allele_type_id][(int)comboBox3.SelectedItem]);
                    }
                    series.LegendText = str + ": allele #" + comboBox3.SelectedItem.ToString();
                    break;
                case "Event frequences":
                    int k = 0;
                    switch (comboBox2.SelectedItem.ToString())
                    {
                        case "Not having progency":
                            k = 1;
                            break;
                    }
                    for (int i = (int)first; i <= last; i++)
                    {
                        series.Points.AddXY(i, popstat[i].event_frequencies[k]);
                    }
                    break;
                case "Phenotype medium values":
                    CharacteristicDescription char_desc = null;
                    foreach (CharacteristicDescription ch in sp.sp_phenotype_table.characteristic_table.Values)
                    {
                        if (ch.name == comboBox2.SelectedItem.ToString())
                            char_desc = ch;
                    }
                    for (int i = (int)first; i <= last; i++)
                    {
                        series.Points.AddXY(i, popstat[i].phenotype_characteristic_medium_values[char_desc.id]);
                    }
                    series.LegendText = str + ": " + char_desc.name;
                    break;
            }            
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox2.Text = null;
            button1.Hide();
            button2.Hide();
            switch (comboBox1.SelectedItem.ToString())
            {
                case "Event frequences":
                    comboBox2.Items.Add("Having progency");
                    comboBox2.Items.Add("Not having progency");
                    comboBox2.Show();
                    label5.Text = "Event type";
                    label5.Show();
                    comboBox3.Hide();
                    label6.Hide();
                    break;
                case "Phenotype medium values":
                    foreach (CharacteristicDescription ch in sp.sp_phenotype_table.characteristic_table.Values)
                    {
                        comboBox2.Items.Add(ch.name);
                    }
                    comboBox2.Show();
                    label5.Text = "Characteristic type";
                    label5.Show();
                    comboBox3.Hide();
                    label6.Hide();
                    break;
                case "Allele frequences":
                    foreach (GeneType gt in sp.sp_gene_table.gene_table)
                    {
                        comboBox2.Items.Add(gt.name);
                    }
                    comboBox2.Show();
                    label5.Text = "Gene type";
                    label5.Show();
                    break;
                default:
                    comboBox2.Hide();
                    label5.Hide();
                    comboBox3.Hide();
                    label6.Hide();
                    draw_chart();
                    button1.Show();
                    button2.Show();
                    break;
            }            
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {            
            comboBox3.Items.Clear();
            comboBox3.Text = null;
            if (comboBox1.SelectedItem.ToString() == "Allele frequences")
            {
                for (int i = 0; i < sp.sp_gene_table.gene_table[comboBox2.SelectedIndex].amount_alelles; i++)
                {
                    comboBox3.Items.Add(i);
                }
                comboBox3.Show();
                label6.Show();
            }
            else if (comboBox2.SelectedItem != null)
            {
                draw_chart();
                button1.Show();
                button2.Show();
            }

        }

        private void GraphicWindow_Load(object sender, EventArgs e)
        {
            chart.Size = new Size(this.Width - 30, this.Height - panel1.Height-100);
            foreach (Species s in model.model_species.Values)
            {
                comboBox4.Items.Add(s.name);
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach(Species s in model.model_species.Values)
            {
                if (comboBox4.SelectedItem.ToString()==s.name)
                    sp=s;
            }
            foreach (Population p in sp.massive_populations.Values)
            {
                comboBox5.Items.Add(p.name);
            }
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Population p in sp.massive_populations.Values)
            {
                if (comboBox5.SelectedItem.ToString() == p.name)
                    pop = p;
            }
            comboBox1.Show();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox3.SelectedItem != null)
            {
                draw_chart();
                button1.Show();
                button2.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            draw_chart();
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            numericUpDown1.Maximum = numericUpDown2.Value - 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.massive_populations[pop.path] = new Population(pop.path, pop.species);
            model.initModelSpecies();
            pop = sp.massive_populations[pop.path];
            done = 0;
            draw_chart();
        }

        private void GraphicWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            sp.massive_populations[pop.path] = new Population(pop.path, pop.species);
            model.initModelSpecies();
            pop = sp.massive_populations[pop.path];
        }        
    }
}
